<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <!-- 表单数据校验 -->
      <!-- el-form绑定 :model属性 对象 表单提交的数据 -->
      <!-- el-form绑定 :rules规则  -->
      <!-- el-input绑定 :v-model绑定双向数据 -->
      <!-- el-form-item 添加prop属性，属性值是model对象里面的属性值 -->
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item
          label="账号"
          prop="username"
        >
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>

        <el-form-item
          label="密码"
          prop="password"
        >
          <el-input v-model="form.password" placeholder="请输入密码" type="password" show-password />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="Login">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { REMEMBER_KEY } from '@/constants/KEY'
export default {
  name: 'Login',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, max: 8, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\w{5,8}$/, message: '密码长度在 5 到 8 个字符', trigger: 'blur' }
        ]
      },
      rememberMe: false
    }
  },
  created() {
    const formData = JSON.parse(localStorage.getItem(REMEMBER_KEY))
    // 判断有没有token
    if (formData) {
      this.form.username = formData.username
      this.form.password = formData.password
    }
  },
  methods: {
    ...mapActions('user', ['doLogin']),
    async Login() {
      // 表单兜底校验
      try {
        await this.$refs.form.validate()
        // 发送登录请求
        await this.doLogin(this.form)
        if (this.rememberMe) {
          // 勾选记住我，保存输入的用户名和密码
          localStorage.setItem(REMEMBER_KEY, JSON.stringify(this.form))
        } else {
          // 没有勾选记住我，清除保存的用户名和密码
          localStorage.removeItem(REMEMBER_KEY)
        }
        // 登录成功提示
        this.$message.success('登录成功')
        // 登录成功后跳转到首页
        const path = this.$route.query.redirect
        if (path) {
          this.$router.push(path)
        } else {
          this.$router.push('/')
        }
      } catch (error) {
        // console.log('登录兜底校验失败', error)
        // 登录失败
        if (error.response) {
          this.$message.error(error.response.data.msg)
        }
      }
    }
  }

}

</script>

<style scoped lang="scss">
  .login_body {
    display: flex;
  }
  .bg {
    width: 60vw;
    height: 100vh;
    background: url('~@/assets/login-bg.svg') no-repeat;
    background-position: right top;
    background-size: cover;
  }
  .box {
    margin: 200px 10% 0;
    flex: 1;
    .title {
      padding-bottom: 76px;
      font-size: 26px;
      font-weight: 500;
      color: #1e2023;
    }
    ::v-deep() {
      .ant-form-item {
        display: flex;
        margin-bottom: 62px;
        flex-direction: column;
      }
      .ant-form-item-label label {
        font-size: 16px;
        color: #8b929d;
      }
      .ant-input,
      .ant-input-password {
        border-radius: 8px;
      }
    }
  }
  .login_btn{
    width: 100%;
  }
</style>
